<?php

declare(strict_types=1);

/**
 * @Author:     Frank
 * @contact     frank_hhm@163.com
 * @Date:       2022-12-28 11:26:49
 * @Last Modified time: 2023-02-23 14:57:36
 */
namespace app\sys\controller\store;

use app\sys\controller\Base;
use think\facade\{
    App
};
use app\common\services\store\GoodsService;
use app\common\services\store\GoodsSkuService;
/**
 * 商城-商品
 * Class Goods 
 * @package app\sys\controller\store
 */
class Goods extends Base
{
    /**
     * Goods constructor.
     * @param App $app
     */
    public function __construct(App $app,GoodsService $service)
    {
        parent::__construct($app);
        $this->service = $service;
    }

    /**
     * 商品列表(详细)
     * @method(GET)
     */
    public function list()
    {
        $param = $this->request->getMore([
            ['goods_name',''],
            ['cate_id',''],
            'destroy'
        ]);
        $this->success($this->service->getGoodsList($param));
    }

    /**
     * 商品详细 
     * @noAuth(true)
     * @method(GET)
     */
    public function detail(){
        if (!$id = $this->request->param('id')) {
            $this->error('参数错误!');
        }
        $this->success($this->service->getGoodsFormDetail($id));
    }

    /**
     * 添加商品
     * @method(POST)
     */
    public function create()
    {
        $data = $this->request->postMore([
            ['cate_id', []],
            ['goods_name', ''],
            ['goods_imgs',[]],
            ['goods_label',[]],
            ['goods_unit',0],
            ['spec_type',1],
            ['content',''],
            ['delivery_type',[]],
            ['delivery_shipping',1],
            ['delivery_shipping_price',''],
            ['delivery_shipping_temp_id',0],
            ['sales_initial',0],
            ['keywords',''],
            ['selling_point',''],
            ['sort', 0],
            ['goods_server',[]],
            ['buy_limit_status',0],
            ['buy_limit',0],
            ['buy_limit_type',0],
            ['buy_least_status',0],
            ['buy_least',0],
            ['buy_least_type',0],

        ]);
        if (!$data['goods_name']) $this->error('请输入商品名称！');
        $spec = $this->request->param('spec');
        if($this->service->createGoods( $data ,$spec)){
            $this->success('添加成功!');
        }
        $this->error('添加失败!');
    }

    /**
     * 编辑商品
     * @method(PUT)
     */
    public function update()
    {
        if (!$id = $this->request->param('id')) {
            $this->error('参数错误!');
        }
        $data = $this->request->postMore([
            ['cate_id', []],
            ['goods_name', ''],
            ['goods_imgs',[]],
            ['goods_label',[]],
            ['goods_unit',0],
            ['spec_type',1],
            ['content',''],
            ['delivery_type',[]],
            ['delivery_shipping',1],
            ['delivery_shipping_price',''],
            ['delivery_shipping_temp_id',0],
            ['sales_initial',0],
            ['keywords',''],
            ['selling_point',''],
            ['sort', 0],
            ['goods_server',[]],
            ['buy_limit_status',0],
            ['buy_limit',0],
            ['buy_limit_type',0],
            ['buy_least_status',0],
            ['buy_least',0],
            ['buy_least_type',0],
        ]);
        if (!$data['goods_name']) $this->error('请输入商品名称！');
        $spec = $this->request->param('spec');
        if($this->service->updateGoods((int)$id,$data ,$spec)){
            $this->success('修改成功!');
        }
        $this->error('修改失败!');
    }


    /**
     * 生成规格
     * @noAuth(true)
     * @method(POST)
     */
    public function createSpec(){

        $data = $this->request->postMore([
            ['spec', []],
        ]);
        if (!$data['spec']) $this->error('规格为空！');
        if($data = GoodsSkuService::instance()->createCacheSpec( $data['spec'] )){
            $this->success('添加成功!',$data);
        }
        $this->error('添加失败!');
    }


    /**
     * 修改商品状态
     * @method(PUT)
     */
    public function status()
    {
        if (!$id = $this->request->param('id')) {
            $this->error('参数错误!');
        }
        if ($this->service->update($id, ['goods_status' => $this->request->param('status')])) {
            $this->service->deleteCache($id);
            $this->success('操作成功');
        }
        $this->error('操作失败');
    }


    /**
     * 商品加入回收站
     * @method(DELETE)
     */
    public function delete()
    {
        if (!$id = $this->request->param('id')) {
            $this->error('参数错误!');
        }

        if ($this->service->destroy((int)$id)) {
            $this->service->deleteCache($id);
            $this->success('删除成功!');
        } 
        $this->error('删除失败!');
    }

    /**
     * 删除商品
     * @method(DELETE)
     */
    public function destroy()
    {
        if (!$id = $this->request->param('id')) {
            $this->error('参数错误!');
        }

        if ($this->service->deleteGoods((int)$id)) {
            $this->success('删除成功!');
        } 
        $this->error('删除失败!');
    }

    /**
     * 商品列表(简单)
     * @noAuth(true)
     * @method(GET)
     */
    public function simpleList()
    {
        $param = $this->request->getMore([
           'cate_id'
        ]);
        $this->success($this->service->getGoodsSimpleList($param));
    }
}
